package com.ikingtech.platform.service.plugin.controller;

import com.ikingtech.framework.sdk.context.exception.FrameworkException;
import com.ikingtech.framework.sdk.core.response.R;
import com.ikingtech.framework.sdk.plugin.core.DelegatePluginRegistrar;
import com.ikingtech.framework.sdk.utils.Tools;
import com.ikingtech.framework.sdk.web.annotation.ApiController;
import com.ikingtech.framework.sdk.web.annotation.PostRequest;
import com.ikingtech.platform.service.plugin.entity.PluginDO;
import com.ikingtech.platform.service.plugin.exception.PluginExceptionInfo;
import com.ikingtech.platform.service.plugin.model.PluginDTO;
import com.ikingtech.platform.service.plugin.model.PluginQueryParamDTO;
import com.ikingtech.platform.service.plugin.service.PluginService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author tie yan
 */
@Slf4j
@RequiredArgsConstructor
@ApiController(value = "/plugin", name = "插件管理", description = "插件管理")
public class PluginController {

    private final PluginService service;

    private final DelegatePluginRegistrar registrar;

    @PostRequest(order = 1, value = "/add", summary = "新增", description = "新增")
    public R<String> add(PluginDTO plugin) {
        if (Boolean.TRUE.equals(this.service.nameExist(plugin.getName()))) {
            throw new FrameworkException(PluginExceptionInfo.DUPLICATE_PLUGIN_NAME);
        }

        PluginDO entity = Tools.Bean.copy(plugin, PluginDO.class);
        entity.setId(Tools.Id.uuid());
        this.service.save(entity);
        return R.ok(entity.getId());
    }

    @PostRequest(order = 2, value = "/delete", summary = "删除", description = "删除")
    @Transactional(rollbackFor = Exception.class)
    public R<Object> delete(String id) {
        this.service.removeById(id);
        return R.ok();
    }

    @PostRequest(order = 3, value = "/update", summary = "更新", description = "更新")
    public R<Object> update(PluginDTO post) {
        if (!this.service.exist(post.getId())) {
            throw new FrameworkException(PluginExceptionInfo.PLUGIN_NOT_FOUND);
        }
        if (this.service.nameExist(post.getId(), post.getName())) {
            throw new FrameworkException(PluginExceptionInfo.DUPLICATE_PLUGIN_NAME);
        }

        this.service.updateById(Tools.Bean.copy(post, PluginDO.class));
        return R.ok();
    }

    @PostRequest(order = 4, value = "/list/page", summary = "分页查询", description = "分页查询")
    public R<List<PluginDTO>> page(PluginQueryParamDTO queryParam) {
        return R.ok(this.service.listPage(queryParam).convert(entity -> Tools.Bean.copy(entity, PluginDTO.class)));
    }

    @PostRequest(order = 5, value = "/list/all", summary = "全量查询", description = "全量查询")
    public R<List<PluginDTO>> all() {
        return R.ok(Tools.Coll.convertList(this.service.list(), entity -> Tools.Bean.copy(entity, PluginDTO.class)));
    }

    @PostRequest(order = 6, value = "/detail", summary = "详情", description = "详情")
    public R<PluginDTO> detail(String id) {
        PluginDO entity = this.service.getById(id);
        if (null == entity) {
            throw new FrameworkException(PluginExceptionInfo.PLUGIN_NOT_FOUND);
        }
        return R.ok(Tools.Bean.copy(entity, PluginDTO.class));
    }
}
